import { message } from 'antd';
import { convertOfdSeavice, sealContractService } from '../services';
import { useState } from 'react';
import commonJS from '@/common/DianJuOFD/common.js';
import { getSessionStrCode } from '@/utils';
import { getBaseUrl } from '@/services/urlconst';

/** 转换ofd文件功能 */
const useOfdWord = (
  resInfo: Record<string, any>,
  reloadManage: () => void,
  uploadFileCallback: (res: Record<string, any>, info: Record<string, any>) => void,
) => {
  /** 保存转换的ofd文件URL */
  const [saveOfdUrl, setSaveOfdUrl] = useState<string>('');

  // /** 设置打印份数回调 */
  // const printCallback = (res: any) => {
  //   if (res?.RET_CODE == 1) {
  //     message.success(res?.MSG)
  //     // commonJS.FilePrint(3); //打印
  //   }
  // }

  // /** 引入打印接口 */
  // const { settingDocPrintNumLoading, settingDocPrintNumRun } = settingPrintNumService(printCallback)

  /** 获取剩余打印份数 */
  // const getPrintCallback = (res: any, savePayload: Record<string, any>) => {
  //   // if (res && res?.code == 1) {
  //   //   // settingDocPrintNumRun({
  //   //   //   ...savePayload,
  //   //   //   contractNum: res?.limit
  //   //   // })
  //   // } else {
  //   //   settingDocPrintNumRun({
  //   //     ...savePayload,
  //   //   })
  //   // }
  //   if (res && res?.code == 0) {
  //     settingDocPrintNumRun({
  //       ...savePayload,
  //     })
  //   }
  //   commonJS.FilePrint(3); //打印
  // }

  /** 引入获取剩余打印份数接口 */
  // const { getDocPrintNumServiceLoading, getDocPrintNumServiceRun } = getDocPrintNumService(getPrintCallback)

  /**
   * 打开点聚插件
   * @param wordUrl 打开文件的 URL
   * @param resObj 打开文件的合同整体信息
   * @param manageClickType 打印打开或盖章打开
   * @param uploadFileCallback  保存文件回调
   */
  const onPrentOFD = (
    wordUrl: string,
    manageClickType: string,
    saveClickInfo: Record<string, any>,
    uploadFileCallback: (res: Record<string, any>, info: Record<string, any>) => void,
  ) => {
    /** 打开文件回调 */
    const OpenFileCallback = (res: any) => {
      if (res != 1) {
        message.error('打开文档失败！');
      } else {
        // // /** 设置文档编号 */
        // if (manageClickType == "sealURL") {
        //   aipServerJs?.DocProperty("DocInterID", saveClickInfo?.contractCode, "", "", "", "")  //设置文档唯一编号
        // }
        //阻止默认打印事件
        // aipServerJs.SetValue('SET_OFD_PERMISSION:Printable', '0');
        // if (manageClickType == 'print') {
        //   getDocPrintNumServiceRun(saveClickInfo)
        // }
      }
    };

    /** 打开文件 */
    commonJS?.OpenFile(
      wordUrl, //访问文件地址
      OpenFileCallback, //打开文件回调
      {
        ...saveClickInfo, //数据详情
        fieldRequest: `${getBaseUrl()}/contractController/uploadFilePoint`, //保存文件盖章接口
        sessionID: `HRKY_SESSION=${getSessionStrCode()}`, //系统 sessionID
        manageClickType, //打印或者盖章
        // tenantName: getTenant(),
        // busiPath: "contract/seal",
        // fieldType: ".docx",
      },
      uploadFileCallback, //保存文件成功回调
      // settingDocPrintNumRun,
    );
  };

  /** 签章接口 */
  const sealCallback = (res: any) => {
    if (res && res?.msgFlag) {
      onPrentOFD(
        saveOfdUrl,
        'sealURL',
        {
          ...res?.obj,
          id: res?.obj?.id,
          versionNum: res?.obj?.versionNum,
          sealURL: res?.obj?.sealURL,
        },
        uploadFileCallback,
      );
      reloadManage();
    } else {
      message.error(res?.msg);
    }
  };

  /**引入合同签章接口 */
  const { sealContractLoading, sealContractRun } = sealContractService(sealCallback);

  /** 转换文件回调 */
  const ofdCallback = (res: any) => {
    if (res?.RET_MSG == 'success') {
      setSaveOfdUrl(res?.FILE_INFO?.FILE_URL);
      sealContractRun({
        sealURL: res?.FILE_INFO?.FILE_URL,
        id: resInfo?.id,
        versionNum: resInfo?.versionNum,
      });
    } else {
      message.error('文件转换失败');
    }
  };

  /** 文件转换 */
  const { convertOfdRun, convertOfdLoading } = convertOfdSeavice(ofdCallback);

  return {
    convertOfdRun,
    convertOfdLoading,
    sealContractRun,
    sealFieldLoading: sealContractLoading,
    onPrentOFD,
    // settingDocPrintNumLoading,
    // getDocPrintNumServiceLoading,
  };
};

export { useOfdWord };
